package server;

import ui.ChatFrame;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ClientReaderThread {
    private Socket socket;
    private DataInputStream dis;
    private ChatFrame win;
    public ClientReaderThread( Socket socket, ChatFrame win) {
        this.win = win;
        this.socket = socket;
    }

    public void run() {
        try {
            dis = new DataInputStream(socket.getInputStream());
            while (true) {
                int messageType = dis.readInt();
                switch (messageType) {
                    case 1:
                        //在线人数更新
                        updateClientOnLineList();

                        break;
                    case 2:

                    case 3:

                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    //在线人数更新
    private void updateClientOnLineList() throws Exception {
        //获取在线人数
        int count = dis.readInt();
        //获取在线用户列表
        List<String> onLineList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String username = dis.readUTF();
            onLineList.add(username);
        }
        win.updateOnlineUsers(onLineList);

    }


    public void start() {
        run();
    }
}
